﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EHTSaleManager.Code.Utility;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.BLL;

namespace EHTSaleManager.UIAdmin
{
    public partial class AdminBannerIU : System.Web.UI.Page
    {
        public Int32 QuangCaoID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                QuangCaoID = Int32.Parse(Request.QueryString["QuangCaoID"].ToString());
            }
            catch(Exception ex)
            {
                QuangCaoID = -1;
            }
            if(QuangCaoID==-1)
            {
                ViewState["action"]=HangSo.ACTION_ADD;
            }
            else
            {
                Quangcao theQuangCao = QuangCaoProvider.GetQuangCaosbyQuangCaosid(QuangCaoID);
                if (theQuangCao == null)
                {
                    Response.Redirect("AdminBanner.aspx");
                }
                else
                {
                    if (!Page.IsPostBack)
                    {
                        LoadBanners();
                    }
                    ViewState["ID"] = theQuangCao.QuangCaoID;
                    ViewState["action"] = HangSo.ACTION_UPDATE;

                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Boolean ValidateData()
        {
            Boolean isSuccess = true;

            try
            {
                if (Int32.Parse(ddlPhanVung.SelectedValue) == -1)
                {
                    isSuccess = false;
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
            }

            return isSuccess;
        }

        /// <summary>
        /// Author : Tung
        /// CreatedDate : 31/12/2012
        /// Description: Load banner quảng cáo
        /// </summary>
        private void LoadBanners()
        {
            Quangcao theQuangCao = QuangCaoProvider.GetQuangCaosbyQuangCaosid(QuangCaoID);
            if (theQuangCao == null)
                return;
            this.ddlPhanVung.SelectedIndex = ddlPhanVung.Items.IndexOf(ddlPhanVung.Items.FindByValue(theQuangCao.QuangCaoID.ToString()));
            this.fckNoiDung.Value = theQuangCao.NoiDung.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateData())
                return;

            Page.Validate();

            if (Page.IsValid)
            {
                Quangcao theQuangCao = new Quangcao();
                theQuangCao.QuangCaoID = QuangCaoID;
                theQuangCao.PhanVungID = Int32.Parse(this.ddlPhanVung.SelectedValue);
                theQuangCao.NoiDung = this.fckNoiDung.Value;
                if (theQuangCao.QuangCaoID == -1)
                    QuangCaoProvider.InsertQuangCaos(theQuangCao, true);
                else
                    QuangCaoProvider.InsertQuangCaos(theQuangCao, false);
                Response.Redirect("AdminBanners.aspx");
            }

        }
        //protected void rpBanners_ItemCommand(object source, RepeaterCommandEventArgs e)
        //{
        //    String cmdName = e.CommandName;
        //    Int32 QuangCaoID = Int32.Parse(e.CommandArgument.ToString());
        //    if (cmdName == "delete1")
        //    {
        //        QuangCaoProvider.GetDeleteQuangCao(QuangCaoID);
        //        LoadBanners();
        //    }

        //}
        /// <summary>
        /// author:tungvu90
        /// </summary>
        /// <param name="sender">Quay tro ve</param>
        /// <param name="e"></param>
        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminBanners.aspx");
        }
    }
}